package com.winit.openapi.wh.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.query.SearchOperator;
import com.winit.common.query.Searchable;
import com.winit.common.query.Sort;
import com.winit.common.query.Sort.Direction;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.inbound.InboundPlannedPackageService;
import com.winit.oms.spi.inbound.command.InboundPlanndPackageCommand;
import com.winit.oms.spi.inbound.vo.InboundPlannedPackageVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

/**
 * 
 * 入单箱单导入表 . 提供的对外接口
 * 
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * hong.jiang 	1.0  		2016-3-21 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/inboundOrder")
public class InboundPlannedPackageController extends BaseController{
	
	@Resource
	InboundPlannedPackageService importPackingListService;
	
	@RequestMapping(value="/plannedPackageList",method=RequestMethod.POST)
	public String queryInboundPackingList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
		Object obj = requestMsg.getData();

        InboundPlanndPackageCommand command = new InboundPlanndPackageCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            ValidateUtil.validMaxLength(json, "orderNo", 60);
            Searchable<?> searchable = buildSearchablePageParams(json);
            searchable.addSearchFilter("ORDER_NO",SearchOperator.eq,json.getString("orderNo"));
            searchable.addSort(new Sort(Direction.DESC, "CREATED"));
            command.setSearchable(searchable);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setCtx(CommandContext.getContext());

        Page<InboundPlannedPackageVo> pagelist = importPackingListService.queryInboundImportPackingList(command);
        if (pagelist != null) {
            List<InboundPlannedPackageVo> datalist = pagelist.getContent();
            
            Map<String, Object> data = getPageParams(pagelist);
            data.put("packingList", datalist);
            responseMsg.setData(data);
        }
        return SUCCESS;
	}
}
